/**
 * @className CartServiceImpl
 * @author liuxuelin
 * @Date 2021/1/19
 * @version v1.0
 */

package com.hidt.shop.service.impl;

import com.hidt.shop.domain.Cart;
import com.hidt.shop.domain.CartAndGoods;
import com.hidt.shop.mapper.CartMapper;
import com.hidt.shop.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sound.midi.Soundbank;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Override
    public int addCart(Cart cart) {
        int i = cartMapper.addCart(cart);
        return i;
    }

    @Override
    public int deleteCartByid(int cart_id) {
        int i = cartMapper.deleteCart(cart_id);
        return i;
    }

    @Override
    public int deleteCartByid2(Cart cart) {
        int i = cartMapper.deleteCart2(cart);
        return i;
    }

    @Override
    public int updateCartByid(Cart cart) {
        int i = cartMapper.updateCartByid(cart);
        return i;
    }

    @Override
    public List<CartAndGoods> findAllCart(int user_id) {
        List<CartAndGoods> listCart = cartMapper.findAllCart(user_id);
        return listCart;
    }


    /**
     *@方法功能描述：根据用户id查找购物表的商品编号和需要加入购物车的对比
     *@methodName findAllCartByUserid
     *@params [user_id]
     *@return java.util.List<com.hidt.shop.domain.Cart>
     *@author 刘雪林
     *@Date 2021/1/19
     */
    @Override
    public int findAllCartByUserid(Cart cart) {
        Cart cartByuser_id = cartMapper.findCartByuser_id(cart);
        System.out.println(cart.getGoods_id());
        int i=0;
//        for (Object listcart : cartByuser_id) {
//            System.out.println(listcart);
//            Cart cart1 = (Cart) listcart;
//            System.out.println(cart1.getGoods_id());
            if (cartByuser_id !=null) {
                i = cartMapper.updateCartBycart_id(cartByuser_id.getCart_id());
                System.out.println("商品已在购物车，数量加1");
            } else {
                i = cartMapper.addCart(cart);
                System.out.println("正在加入购物车");

            }
//        }
      return i;
    }

    /**
     *@方法功能描述：把购物车的商品数加一
     *@methodName updateCartBycart_id
     *@params [cart_id]
     *@return int
     *@author 刘雪林
     *@Date 2021/1/27
     */
    @Override
    public int updateCartBycart_id(int cart_id) {
        int i = cartMapper.updateCartBycart_id(cart_id);
        return i;
    }
}
